package com.TeamJigsaw.DanteTrail.Views;

import java.awt.Color;
import java.awt.Dimension;
import java.awt.Graphics;
import java.awt.Image;
import java.awt.Point;
import java.util.List;

import javax.swing.ImageIcon;
import javax.swing.JPanel;

import com.TeamJigsaw.DanteTrail.Models.Checkpoint;

public class MapScreenDisplay extends JPanel{

	private final int SCREEN_WIDTH = 500;
	private final int SCREEN_HEIGHT = 300;
	private int location;
	private Image skullImg;
	private Image mapBG;
	private Image player;
	private List<Checkpoint> checkpoints;
	
	/**
	 * Creates and initializes the MapScreenDisplay
	 * @param player The image of the player
	 * @param location The location of the player
	 * @param checkpoints List of checkpoints representing the map
	 */
	public MapScreenDisplay(Image player, int location, List<Checkpoint> checkpoints) {
		this.checkpoints = checkpoints;
		this.location = location;
		this.player = player;
		skullImg = new ImageIcon("resources\\skullMapIcon.gif").getImage();
		mapBG = new ImageIcon("resources\\mapBG.gif").getImage();
		
		setPreferredSize(new Dimension(500,300));
	}
	
	/**
	 * Paints to the graphics object
	 * @param g The graphics object to paint onto
	 */
	@Override
	public void paint(Graphics g) {
		g.setColor(Color.BLACK);
		
		//draw the map background
		g.drawImage(mapBG, 0, 0, null);
		
		//create variables to store checkpoint locations for drawing
		Point prevPos = null;
		Point curPos = null;
		
		int i = 0;
		
		g.setColor(Color.RED);
		
		//draw all the checkpoints
		for (Checkpoint cur : checkpoints) {
			//get the position of the
			curPos = getPoint(cur.getLocation());
			
			//check to see if checkpoint is special case first town
			//if not special case, draw the line connecting towns
			if (cur != checkpoints.get(0)) {
				g.drawLine(prevPos.x, prevPos.y, curPos.x, curPos.y);
			}
			
			//set prevPos to the curPos
			prevPos = curPos;
			
			//draw the skull icon
			g.drawImage(skullImg, curPos.x, curPos.y, null);
			
			//draw the name of the checkpoint
			//notice the garbage checks I am doing to get the panel pretty
			if (i < 3 || i == 8 || i == 13)
				g.drawString(cur.getName(), curPos.x, curPos.y);
			else
				g.drawString(cur.getName(), curPos.x - (cur.getName().length() * 5) - 8, curPos.y+32);
			i++;
		}
		
		//draw the player
		Point playerPos = getPoint(location);
		g.drawImage(player, playerPos.x, playerPos.y, null);
	}
	
	/**
	 * Returns a coordinate point given a distance location
	 * @param distance A distance location from the start of the trail
	 * @return A point representing the distance value
	 */
	private Point getPoint(int distance) {
		int x = (int) (Math.sqrt(distance) * 11 + Math.sqrt(distance)) - 100;
		int y = (int) (Math.sqrt(distance) * 7) - 50;
		return (new Point(x, y));
	}
}
